/**
 * 预览附加子用户的图片预览
 */
function imgContainerView(){
    //预览图片
    var config = {
        tip: "请上传格式为png, gif或者jpg的图片",
        fileId: "imgSubsidiaryFile", //文件上传 input 的那个 id
        containerId: "containerImg", //预览图片的容器的ID
        imgStyle: "width:75px;height:75px;" //预览图的样式
    };
    var previewer = new ImgPrevirewer(config);
    previewer.preview();
}



/**
 * 显示子用户详情
 * @param id
 * @param name
 */
function showingSonUserDetails(id, name) {
    if (checkNull(id)){
        return;
    }

    //加载层
    var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
    var url = "/user/showingSonUserDetails.php";
    $.ajax({
        url: url,
        type:"get",
        dataType:"json",
        data:{"sonUserId" : id, "type" : "1"},
        success:function(data){
            if (data.status == "success"){
                //数据进行回显
                var obj = data.object;
                if (obj != null){
                    var fatherSub = obj.fatherSub;
                    var subsidiaryUser = obj.subsidiaryUser;
                    var relationship = obj.relationship;

                    if (subsidiaryUser != null){
                        clearForm('SubsidiaryUserForm');

                        var imgHred = subsidiaryUser.imgHred;
                        if (!checkNull(imgHred)){
                            imgHred = "<img src=\"/user/getSonImgHred?id=" + imgHred + "\" style=\"width:75px;height:75px;\">"
                        } else {
                            imgHred = "<img src=\"/img/common/worker.png\" style=\"width:75px;height:75px;\">"
                        }
                        replaceAppoint("imgHredDetail", imgHred);
                        replaceAppoint("nameDetail", subsidiaryUser.name);
                        var sex = subsidiaryUser.sex;
                        if (sex == "1"){
                            sex = "男";
                        } else if(sex == "2"){
                            sex = "女";
                        } else {
                            sex = "";
                        }

                        //省市县的回显
                        var province = subsidiaryUser.province;
                        var city = subsidiaryUser.city;
                        var county = subsidiaryUser.county;
                        if (!checkNull(province)){
                            for (var i = 0; i < areas.length; i ++){
                                var area = areas[i];
                                if (parseInt(area.level) == 1){
                                    if (parseInt(province) == parseInt(area.code)){
                                        replaceAppoint("provinceDetail", area.name);
                                    }
                                }
                                if (parseInt(province) == parseInt(area.parentCode)){
                                    if (parseInt(city) == parseInt(area.code)){
                                        replaceAppoint("cityDetail", area.name);
                                    }
                                }
                                if (parseInt(city) == parseInt(area.parentCode)){
                                    if (parseInt(county) == parseInt(area.code)){
                                        replaceAppoint("countyDetail", area.name);
                                    }
                                }
                            }
                        }

                        replaceAppoint("sexDetail", sex);
                        replaceAppoint("birthdayDetail", dateFormat(subsidiaryUser.birthday));
                        replaceAppoint("ethnicDetail", subsidiaryUser.ethnicStr);
                        replaceAppoint("addDetailsDetail", subsidiaryUser.addDetails);
                        replaceAppoint("schoolDetail", subsidiaryUser.school);
                        replaceAppoint("professionDetail", subsidiaryUser.profession);
                        replaceAppoint("emailDetail", subsidiaryUser.email);
                        replaceAppoint("identityDetail", subsidiaryUser.identity);
                        replaceAppoint("occupationDetail", subsidiaryUser.occupation);
                        replaceAppoint("industryDetail", subsidiaryUser.industry);
                        var isParty = subsidiaryUser.isParty;
                        if (isParty == "1"){
                            isParty = "是党员";
                        } else if(isParty == "2"){
                            isParty = "非党员";
                        } else {
                            isParty = "";
                        }
                        replaceAppoint("isPartyDetail", isParty);
                        replaceAppoint("addPartyDetail", dateFormat(subsidiaryUser.addParty));
                        var loveState = subsidiaryUser.loveState;
                        if (loveState == "1"){
                            loveState = "未婚";
                        } else if(loveState == "2"){
                            loveState = "已婚";
                        } else if(loveState == "3"){
                            loveState = "离异";
                        } else if(loveState == "4"){
                            loveState = "再婚";
                        } else {
                            loveState = "";
                        }
                        replaceAppoint("loveStateDetail", loveState);
                        replaceAppoint("remarkDetail", subsidiaryUser.remark);

                        //回显关系
                        if (fatherSub != null && relationship != null){
                            var relationshipDetail = "<p><span style='color: red'><b> " + subsidiaryUser.name + "</b></span> 与 <span style='color: red'><b> " + fatherSub.name + " </b></span> 的关系为: <span style='color: red'><b> " + relationship.explain +  " </b></span></p>";
                            replaceAppoint("relationshipDetail", relationshipDetail);
                        }
                    }
                }

                //JS触发锚点
                window.location.hash = "#anchorDetails";

            } else if (data.status == 'error'){
                alert(data.msg);
            } else {
                alert("未知情况");
            }
            //关闭弹出层
            layer.close(index);
        },
        error:function(e){
            //关闭弹出层
            layer.close(index);
            alert("错误！！" + e);
        }

    });
}

/**
 * 添加附属子用户
 * @param id 上级用户的ID
 * @param name 上级用户的名字
 */
function addSubsidiary(id, name) {
    $("#father").val(id);
    $("#fatherName").val(name);
    //JS触发锚点
    window.location.hash = "#addAnchor";
}


/**
 * 修改附属子用户的前期工作，回显数据
 * @param id 当前用户
 */
function updSubsidiary(id) {
    if (checkNull(id)){
        alert("ID获取失败");
        return;
    }

    //加载层
    var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
    var url = "/user/showingSonUserDetails.php";
    $.ajax({
        url: url,
        type:"get",
        dataType:"json",
        data:{"sonUserId" : id, "type" : "2"},
        success:function(data){
            if (data.status == "success"){
                //数据进行回显
                var obj = data.object;
                if (obj != null){
                    var fatherSub = obj.fatherSub;
                    var subsidiaryUser = obj.subsidiaryUser;
                    var relationship = obj.relationship;
                    if (subsidiaryUser != null){
                        clearForm('SubsidiaryUserForm');

                        $("#subsidiaryUserId").val(subsidiaryUser.id);
                        $("#relationshipId").val(relationship.id);
                        if (fatherSub != null){
                            $("#father").val(fatherSub.id);
                            $("#fatherName").val(fatherSub.name);
                        }
                        var imgHred = subsidiaryUser.imgHred;
                        if (!checkNull(imgHred)){
                            imgHred = "<img src=\"/user/getSonImgHred?id=" + imgHred + "\" style=\"width:75px;height:75px;\">"
                        } else {
                            imgHred = "<img src=\"/img/common/worker.png\" style=\"width:75px;height:75px;\">"
                        }
                        replaceAppoint("containerImg", imgHred);
                        if (relationship != null){
                            if (relationship.level == "1"){
                                redioCheck("level", "1");
                            } else if (relationship.level == "2"){
                                redioCheck("level", "2");
                            } else if (relationship.level == "3"){
                                redioCheck("level", "3");
                            } else if (relationship.level == "4"){
                                redioCheck("level", "4");
                            } else if (relationship.level == "5"){
                                redioCheck("level", "5");
                            } else if (relationship.level == "6"){
                                redioCheck("level", "6");
                            } else if (relationship.level == "7"){
                                redioCheck("level", "7");
                            } else if (relationship.level == "8"){
                                redioCheck("level", "8");
                            }
                        }
                        $("#name").val(subsidiaryUser.name);
                        $("#explain").val(relationship.explain);
                        if (subsidiaryUser.sex == "1"){
                            redioCheck("sex", "1");
                        } else if (subsidiaryUser.sex == "2"){
                            redioCheck("sex", "2");
                        }
                        $("#birthday").val(dateFormat(subsidiaryUser.birthday));

                        //民族
                        if (!checkNull(subsidiaryUser.ethnic) && subsidiaryUser.ethnic != "-1"){
                            replaceAppoint("ethnic", "<option class='selectOpenStyle' value='" + subsidiaryUser.ethnic + "'>" + subsidiaryUser.ethnicStr + "</option>")
                        }

                        //省市县
                        var province = subsidiaryUser.province;
                        var city = subsidiaryUser.city;
                        var county = subsidiaryUser.county;
                        $("#provinceData").val(province);
                        $("#cityData").val(city);
                        $("#countyData").val(county);
                        provinceInit();
                        cityInit();
                        countyInit();
                        $("#provinceData").val("");
                        $("#cityData").val("");
                        $("#countyData").val("");

                        $("#addDetails").val(subsidiaryUser.addDetails);
                        if (subsidiaryUser.loveState == "1"){
                            redioCheck("loveState", "1");
                        } else if (subsidiaryUser.loveState == "2"){
                            redioCheck("loveState", "2");
                        } else if (subsidiaryUser.loveState == "3"){
                            redioCheck("loveState", "3");
                        } else if (subsidiaryUser.loveState == "4"){
                            redioCheck("loveState", "4");
                        }
                        $("#school").val(subsidiaryUser.school);
                        $("#profession").val(subsidiaryUser.profession);
                        $("#email").val(subsidiaryUser.email);
                        $("#identity").val(subsidiaryUser.identity);
                        $("#industry").val(subsidiaryUser.industry);
                        $("#occupation").val(subsidiaryUser.occupation);
                        if (subsidiaryUser.isParty == "1"){
                            redioCheck("isParty", "1");
                        } else if (subsidiaryUser.isParty == "2"){
                            redioCheck("isParty", "2");
                        }
                        $("#addParty").val(dateFormat(subsidiaryUser.addParty));
                        $("#remark").val(subsidiaryUser.remark);

                    }

                    //JS触发锚点
                    window.location.hash = "#anchorDetails";

                } else {
                    alert("主用户不能在此处进行更新操作。");
                }



            } else if (data.status == 'error'){
                alert(data.msg);
            } else {
                alert("未知情况");
            }
            //关闭弹出层
            layer.close(index);
        },
        error:function(e){
            //关闭弹出层
            layer.close(index);
            alert("错误！！" + e);
        }

    });


    //JS触发锚点
    window.location.hash = "#addAnchor";
}

/**
 * 检查子用户添加 数据是否完整
 * @returns {boolean} false 验证失败  true 验证成功
 */
function checkSub(){
    var userId = $("#userId").val();
    if (checkNull(userId)){
        alert("附属用户ID获取失败");
        return false;
    }
    var father = $("#father").val();
    if (checkNull(father)){
        alert("父用户ID获取失败");
        return false;
    }

    var level = $('input:radio[name="level"]:checked').val();
    if(level == null){
        alert("请选择 用户关系");
        return false;
    }
    var explain = $("#explain").val();
    if (checkNull(explain)){
        alert("请填写用户关系说明");
        return false;
    }
    var name = $("#name").val();
    if (checkNull(name)){
        alert("请填写用户名字");
        return false;
    }
    return true;
}

/**
 * 新增附属用户
 */
function addOrUpdSubsidiaryUser() {
    if (!checkSub()){
        return;
    }

    //注意，这里是获取对象，不是用值
    var form = document.getElementById("SubsidiaryUserForm");
    var formData = new FormData(form);
    //加载层
    var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2

    var url = "/user/addOrUpdSubsidiaryUser.php";
    $.ajax({
        url: url,
        type:"post",
        dataType:"json",
        data:formData,
        processData: false,  // 告诉jQuery不要去处理发送的数据
        contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
        success:function(data){
            if (data.status == "success"){
                alert(data.msg);
                clearForm('SubsidiaryUserForm');
                //JS触发锚点
                window.location.hash = "#anchorTop";
                //刷新当前页
                location.reload();

            } else if (data.status == 'error'){
                alert(data.msg);
            } else {
                alert("未知情况");
            }
            //关闭弹出层
            layer.close(index);
        },
        error:function(e){
            //关闭弹出层
            layer.close(index);
            alert("错误！！" + e);
        }

    });
}


/**
 * 根据子用户id删除用户
 * @param id
 */
function delSubsidiaryUser(id) {
    if (checkNull(id)){
        alert("删除参数异常");
        return;
    }

    //加载层
    var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2

    var url = "/user/delSubsidiaryUser.php";
    $.ajax({
        url: url,
        type:"get",
        dataType:"json",
        data:{"id" : id},
        success:function(data){
            if (data.status == "success"){
                alert(data.msg);
                //刷新当前页
                location.reload();

            } else if (data.status == 'error'){
                alert(data.msg);
            } else {
                alert("未知情况");
            }
            //关闭弹出层
            layer.close(index);
        },
        error:function(e){
            //关闭弹出层
            layer.close(index);
            alert("错误！！" + e);
        }

    });
}